<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends CI_Controller {
    function Page() 
    {
        parent::__construct();
	//$this->load->scaffolding('entries');
	$this->load->helper('html');
	$this->load->helper('url');
	$this->load->helper('form');
    }
	
    public function finance()
    {
        $this->load->model('Page_Model');
        $data['make'] = $this->db->get('psacar_make'); 
        $data['currentpage'] = $this->Page_Model->GetPage("finance");

        foreach($data['currentpage']->result() as $row){
            $data['pageslist'] = $this->Page_Model->GetPageListByParent($row->Id);
        }
        
        $data['subtitle'] = 'Finance'; 
        $this->load->view('header');
        $this->load->view('page', $data);
        $this->load->view('footer');
    }

    public function insurance()
    {
        $this->load->model('Page_Model');
        $data['make'] = $this->db->get('psacar_make'); 
        $data['currentpage'] = $this->Page_Model->GetPage("insurance");

        foreach($data['currentpage']->result() as $row){
                $data['pageslist'] = $this->Page_Model->GetPageListByParent($row->Id);
        }
        
        $data['subtitle'] = 'Insurance';
        $this->load->view('header');
        $this->load->view('page',$data);
        $this->load->view('footer');
    }

    public function contactus()
    {
        $this->load->model('Page_Model');
        $data['make'] = $this->db->get('psacar_make'); 
        $data['currentpage'] = $this->Page_Model->GetPage("contact-us");

        foreach($data['currentpage']->result() as $row){
            $data['pageslist'] = $this->Page_Model->GetPageListByParent($row->Id);
        }
        
        $data['subtitle'] = '';
        $data['alertmessage'] = '';

        $this->load->view('header');
        $this->load->view('templates/page/contactus',$data);
        $this->load->view('footer');
    }
    
    public function contactus_mail() {
        $data['message'] = "";
        $this->load->view('templates/page/contactus_mail', $data);
    }

    public function contactus_map() {
        $data['message'] = "";
        $this->load->view('templates/page/contactus_map', $data);
    }

    public function aboutus()
    {
        $this->load->model('Page_Model');
        $data['make'] = $this->db->get('psacar_make'); 
        $data['currentpage'] = $this->Page_Model->GetPage("about-us");

        foreach($data['currentpage']->result() as $row)
        {
            $data['pageslist'] = $this->Page_Model->GetPageListByParent($row->Id);
        }
        $data['subtitle'] = '';

        $this->load->view('header');
        $this->load->view('templates/page/aboutus', $data);
        $this->load->view('footer');
    }

    public function financedetail()
    {
        $Id = (int)($this->uri->segment(3, 0));
        $this->load->model('Page_Model');
        $data['make'] = $this->db->get('psacar_make'); 
        $data['pagedetail'] = $this->Page_Model->GetPageById($Id);

        $this->load->view('header');
        $this->load->view('templates/page/page_detail',$data);
        $this->load->view('footer');
    }

    public function insurancedetail()
    {
        $Id = (int)($this->uri->segment(3, 0));
        $this->load->model('Page_Model');
        $data['make'] = $this->db->get('psacar_make'); 
        $data['pagedetail'] = $this->Page_Model->GetPageById($Id);

        $this->load->view('header');
        $this->load->view('page_details_view',$data);
        $this->load->view('footer');
    }
    
    public function sitemap()
    {
        $data['message'] = '';
        $data['make'] = $this->db->get('psacar_make'); 
        
        $this->load->view('header');
        $this->load->view('templates/page/sitemap', $data);
        $this->load->view('footer');        
    }
    
    public function sendmail() {
        if(isset($_POST['email'])) {            
            //Mandatory
            $first_name = $_POST['first_name'];
            $last_name = $_POST['last_name'];
            $from = $_POST['email'];
            $comments = $_POST['comments'];

            //Optional
            $telephone = $_POST['telephone'];

            // validation expected data exists
            /*
            if(!isset($_POST['first_name']) ||
                !isset($_POST['last_name']) ||
                !isset($_POST['email']) ||
                !isset($_POST['telephone']) ||
                !isset($_POST['comments'])) 
            {
                died('We are sorry, but there appears to be a problem with the form you submitted.');       
            }
            */

            $to = "contact@psacar.com";
            $subject = "Your email subject line";

            $message = "Form details below.\n\n";
            $message .= "First Name: " . $first_name . "\n";
            $message .= "Last Name: " . $last_name . "\n";
            $message .= "Email: " . $from . "\n";
            $message .= "Telephone: " . $telephone . "\n";
            $message .= "Comments: " . $comments ."\n";

            //$data['alertmessage'] = 'Thank you for contacting us. We will be in touch with you very soon.';
            
            $this->load->library('email');

            $this->email->from($from);
            $this->email->to($to);             
            $this->email->bcc('sakal.yim@gmail.com'); 

            $this->email->subject($subject);
            $this->email->message($message);	

            $this->email->set_mailtype("html");
            $this->email->send();            
        }       
    }
}